<template>
	<div class="integralRules-box">
		<avue-form ref="form" :option="option" v-model="form" @submit="handleSubmit">
			<template slot="group1Header">
				<h1 class="avue-group__title">
					<i class="el-icon-info avue-group__icon"></i>项目赠送
				</h1>
			</template>
			<template slot-scope="scope" slot="dateScope">
				<el-col>
					<el-form-item label="" style="width: 40%;display: inline-block;">
						<el-date-picker placeholder="开始时间" @change="getBeginTime" :picker-options="pickerOptions" value-format="yyyy-MM-dd"  v-model="form.integral_begin" type="date">
						</el-date-picker>
					</el-form-item>
					至
					<el-form-item label="" style="width: 40%;display: inline-block;">
						<el-date-picker placeholder="结束日期" @change="getEndTime" :picker-options="pickerOptions" value-format="yyyy-MM-dd" v-model="form.integral_end" type="date">
						</el-date-picker>
					</el-form-item>
				</el-col>
			</template>
		</avue-form> 
	</div>
</template>

<script>
	import {
		update,
		getIntegralRule
	}
	from "@/api/base/operations/integralCenter/integralRules";
	import {
		validatPrice,
		validatPrice1
	} from '@/util/validate'
	export default {
		data() {
			return {
				pickerOptions: {
					disabledDate(time) {
						return time.getTime() + 3600 * 1000 * 24 * 1 < Date.now();
					},
				},
				form: {},
				option: {
					emptyBtn:false,
					group: [{
							icon: 'el-icon-info',
							label: '项目赠送',
							labelWidth: 95,
							prop: 'group1',
							column: [{
									label: '会员注册',
									prop: 'memberRegister',
									tip: '注册会员获得积分数',
									rules: [
										{
										required: true,
										message: "请输入注册会员获得积分数",
										trigger: "blur"
									}],
									blur: () => {
										let  value=this.form.memberRegister
										console.log(value)
										if(value==''){
											// this.$message.error('请输入注册会员获得积分数')
											return
										}
										console.log(validatPrice1(value))
										if(!validatPrice1(value)) {
											if(value&&Number(value) >2147483647){
												this.$message.error('注册会员获得积分最大为2147483647')
												this.form.memberRegister=null
												return
											}else{
												this.form.memberRegister=null
												this.$message.error('注册会员获得积分数必须为大于0的整数！')
												return
											}
										}
										if(value&&Number(value) <1){
											this.$message.error('注册会员最少可获得1个积分！')
											this.form.memberRegister=null
											return
										}
										
									}
								},
								{
									label: '会员每天登录',
									labelWidth:120,
									prop: 'memberDayLogin',
									tip: '每天登录获得积分数',
									rules: [{
										required: true,
										message: "请输入每天登录获得积分数",
										trigger: "blur"
									}],
									blur: () => {
										let  value=this.form.memberDayLogin
										if(value==''){
											// this.$message.error('请输入注册会员获得积分数')
											return
										}
										if(!validatPrice1(value)) {
											if(value&&Number(value) >2147483647){
												this.$message.error('每天登录获得积分最大为2147483647')
												this.form.memberDayLogin=null
												return
											}else{
												this.form.memberDayLogin=null
												this.$message.error('每天登录获得积分数必须为大于0的整数！')
												return
											}
										}
										if(value&&Number(value) <1){
											this.$message.error('每天登录最少可获得1个积分！')
											this.form.memberDayLogin=null
											return
										}
										
									}
								},
								{
									label: '订单商品评论',
									labelWidth: 120,
									prop: 'indentComment',
									tip: '评论商品获得积分数',
									rules: [{
										required: true,
										message: "请输入评论商品获得积分数",
										trigger: "blur"
									}],
									blur: () => {
										let value = this.form.indentComment
										if (value == '') {
											// this.$message.error('请输入注册会员获得积分数')
											return
										}
										if (!validatPrice1(value)) {
											if (value && Number(value) > 2147483647) {
												this.$message.error('评论商品获得积分最大为2147483647')
												this.form.indentComment = null
												return
											} else {
												if (value && Number(value) > 2147483647) {
													this.$message.error('评论商品获得积分最大为2147483647')
													this.form.indentComment = null
													return
												}
											}
											if (value && Number(value) < 1) {
												this.$message.error('评论商品获得积分数最少可获得1个积分！')
												this.form.indentComment = null
												return
											}

										}
									}
								}
							]
						},
						{
							icon: 'el-icon-info',
							label: '购物赠送',
							labelWidth: 160,
							prop: 'group2',
							column: [{
									label: '购物积分',
									labelWidth:95,
									prop: 'consumptionRatio',
									tip: '订单金额：积分，如：10，表示10货币赠送1积分',
									rules: [{
										required: true,
										message: "请输购物积分",
										trigger: "blur"
									}],
									blur: () => {
										let  value=this.form.consumptionRatio
										if(value==''){
											// this.$message.error('请输入注册会员获得积分数')
											return
										}
										if(!validatPrice1(value)) {
											if(value&&Number(value) >2147483647){
												this.$message.error('购物积分最大为2147483647')
												this.form.consumptionRatio=null
												return
											}else{
												this.form.consumptionRatio=null
												this.$message.error('购物获得积分数必须为大于0的整数！')
												return
											}
										}
										if(value&&Number(value) <1){
											this.$message.error('购物积分最少送1个积分！')
											this.form.consumptionRatio=null
											return
										}
									}
								},
								{
									label: '购物积分上限',
									labelWidth:120,
									prop: 'everyIndentLimit',
									tip: '每个订单最多赠送积分数',
									rules: [{
										required: true,
										message: "请输入购物积分上限",
										trigger: "blur"
									}],
									blur: () => {
										let  value=this.form.everyIndentLimit
										if(value==''){
											// this.$message.error('请输入注册会员获得积分数')
											return
										}
										if(!validatPrice1(value)) {
											if(value&&Number(value) >2147483647){
												this.$message.error('购物积分上限最大为2147483647')
												this.form.everyIndentLimit=null
												return
											}else{
												this.form.everyIndentLimit=null
												this.$message.error('购物积分上限必须为大于0的整数！')
												return
											}
										}
										if(value&&Number(value) <1){
											this.$message.error('购物积分上限至少为1个积分！')
											this.form.everyIndentLimit=null
											return
										}
										
									}
								}
							]
						},
						{
							icon: 'el-icon-info',
							label: '积分翻倍',
							prop: 'group2',
							column: [{
									label: "增加积分的日期范围",
								    labelWidth:160,
									prop: "dateScope",
									formslot: true,
									// rules: [{
									// 	required: true,
									// 	message: "请选择增加积分的日期范围",
									// 	trigger: "change"
									// }],
								},
								{
									label: '增加积分的倍数',
									labelWidth:130,
									prop: 'integral_multiple',
									rules: [{
										required: true,
										message: "请输入增加积分的倍数",
										trigger: "blur"
									}],
									blur: () => {
										let  value=this.form.integral_multiple
										if(value==''){
											// this.$message.error('请输入注册会员获得积分数')
											return
										}
										if(!validatPrice1(value)) {
											if(Number(value) >2147483647){
												this.$message.error('增加积分的倍数最大为2147483647')
												this.form.integral_multiple=null
												return
											}else{
												this.form.integral_multiple=null
												this.$message.error('增加积分的倍数必须为大于0的整数！')
												return
											}
											
										}
										if(value&&Number(value) <1){
											this.$message.error('增加积分的倍数至少为1！')
											this.form.integral_multiple=null
											return
										}
									}
								}
							]
						}
					]
				},
			}
		},
		mounted: function() {
			this.getIntegralRule();
		},
		methods: {
			getBeginTime(){
				if(!this.form.integral_begin) {
					this.$message({
						type: "error",
						message: "请选择积分翻倍开始时间"
					})
					done()
					return
				}
				let nowDate=new Date().toLocaleDateString().replace(/\//g, "-")
				// if(new Date(this.form.integral_begin) < new Date(nowDate) && this.form.integral_begin) {
				// 	this.$message({
				// 		type: "error",
				// 		message: "开始时间不能小于当前时间"
				// 	})
				// 	//this.form.integral_begin = ''
				// 	done()
				// 	return
				// }
				if(this.form.integral_end){
					if (this.form.integral_begin >this.form.integral_end ) {
						this.$message({
							type: "error",
							message: "开始时间不能大于结束时间"
						});
						//this.form.integral_begin = '' r
						return
					}
				}
				
				//为了提交时能过必填的校验
				if(this.form.integral_begin && this.form.integral_end ){
					this.dateScope="true";
				}
			},
			getEndTime(){
				if(!this.form.integral_end) {
					this.$message({
						type: "error",
						message: "请选择积分翻倍结束时间"
					})
					done()
					return
				}
				let nowDate=new Date().toLocaleDateString().replace(/\//g, "-")
				console.log(new Date(this.form.integral_end),new Date(nowDate))
				// if(new Date(this.form.integral_end) < new Date(nowDate)) {
				// 	this.form.integral_end = ''
				// 	this.$message({
				// 		type: "error",
				// 		message: "开始时间不能小于当前时间"
				// 	})
				// 	done()
				// 	return
				// }
				if(this.form.integral_begin){
					if (this.form.integral_begin >this.form.integral_end ) {
						this.$message({
							type: "error",
							message: "结束时间不能小于开始时间"
						});
						//this.form.integral_end = ''
						return
					}
				}
				
				//为了提交时能过必填的校验
				if(this.form.integral_begin && this.form.integral_end){
					this.dateScope="true";
				}
			},
			handleSubmit(form, done) {
				if(!this.form.integral_begin || !this.form.integral_end){
					this.$message({
						type: "error",
						message: "请选择积分翻倍时间"
					});
					done()
					return;
				}
				let nowDate=new Date().toLocaleDateString().replace(/\//g, "-")
				// if(new Date(this.form.integral_begin) < new Date(nowDate)) {
				//
				// 	this.$message({
				// 		type: "error",
				// 		message: "开始时间不能小于当前时间"
				// 	})
				// 	//this.form.integral_begin=''
				// 	done()
				// 	return
				// }

				// if(new Date(this.form.integral_end) < new Date(nowDate)) {
				// 	this.$message({
				// 		type: "error",
				// 		message: "结束时间不能小于当前时间"
				// 	})
				// 	done()
				// 	//this.form.integral_end=''
				// 	return
				// }


				if (this.form.integral_begin >this.form.integral_end ) {
					this.$message({
						type: "error",
						message: "开始时间不能大于结束时间"
					});
					//this.form.integral_end = ''
					done()
					return;
				}
				if(!this.form.integral_multiple || !this.form.everyIndentLimit || !this.form.consumptionRatio || !this.form.indentComment || !this.form.memberDayLogin || !this.form.memberRegister) {
					this.$message({
						type: "error",
						message: "请输入完整积分"
					});
					done()
					return;
				}
				if(!validatPrice1(this.form.integral_multiple) || !validatPrice1(this.form.everyIndentLimit) || !validatPrice1(this.form.consumptionRatio) || !validatPrice1(this.form.indentComment) || !validatPrice1(this.form.memberDayLogin) || !validatPrice1(this.form.memberRegister)) {
					this.$message({
						type: "error",
						message: "积分数为大于0的整数"
					});
					done()
					return;
				}
				console.log(this.form);
				update(this.form).then(res => {
					if (res.data.code == 200) {
						this.$message({
							type: "success",
							message: "添加成功"
						});
					} else {
						this.$message({
							type: "error",
							message: res.data.message
						});
					}
				});
				done()
			},
			getIntegralRule() {
				getIntegralRule().then(res => {
					if (res.status == 200) {
						if (res.data.code == 200) {
							//前台日期数据格式转化为后台可接收数据格式
							let data = res.data.data
							this.form = data
							console.log(this.form)
						} else {
							this.$message.error(res.data.message);
						}
						if(this.form.integral_begin != null || this.form.integral_end != null){
							this.form.dateScope="true";
						}
					}
				});
			}

		}
	}
</script>

<style scoped>
</style>
